﻿
using System.IO;

namespace LS.Editor.DataTableTools
{
    public sealed partial class DataTableProcessor
    {
        private sealed class BooleanProcessor : GenericDataProcessor<bool>
        {
            public override bool IsSystem
            {
                get
                {
                    return true;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "bool";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "bool",
                    "boolean",
                    "system.boolean"
                };
            }

            public override bool Parse(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return default;
                }
                return bool.Parse(value);
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                binaryWriter.Write(Parse(value));
            }
        }
        private sealed class BooleanArrayProcessor : GenericDataArrayProcessor<bool>
        {
            public override bool IsSystem
            {
                get
                {
                    return false;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "bool[]";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "bool[]",
                    "boolean[]",
                    "system.boolean[]",
                };
            }

            public override bool ParseItem(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return default;
                }
                return bool.Parse(value);
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                var array = Parse(value);
                binaryWriter.Write(array.Length);
                for (int i = 0; i < array.Length; i++)
                {
                    binaryWriter.Write(array[i]);
                }
            }
        }
        private sealed class BooleanListProcessor : GenericDataListProcessor<bool>
        {
            public override bool IsSystem
            {
                get
                {
                    return false;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "List<bool>";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "list<bool>",
                    "list<boolean>",
                    "list<system.boolean>",
                    "system.collections.generic.list<bool>",
                    "system.collections.generic.list<boolean>",
                    "system.collections.generic.list<system.boolean>",
                };
            }

            public override bool ParseItem(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return default;
                }
                return bool.Parse(value);
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                var array = Parse(value);
                binaryWriter.Write(array.Count);
                for (int i = 0; i < array.Count; i++)
                {
                    binaryWriter.Write(array[i]);
                }
            }
        }
        private sealed class BooleanDictionaryProcessor : GenericDataDictionaryProcessor<bool>
        {
            public override bool IsSystem
            {
                get
                {
                    return false;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "Dictionary<string,bool>";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "dictionary<string,bool>",
                    "dictionary<string,boolean>",
                    "dictionary<string,system.boolean>",
                    "dictionary<system.string,bool>",
                    "dictionary<system.string,boolean>",
                    "dictionary<system.string,system.boolean>",
                    "system.collections.generic.dictionary<string,bool>",
                    "system.collections.generic.dictionary<string,boolean>",
                    "system.collections.generic.dictionary<string,system.boolean>",
                    "system.collections.generic.dictionary<system.string,bool>",
                    "system.collections.generic.dictionary<system.string,boolean>",
                    "system.collections.generic.dictionary<system.string,system.boolean>",
                };
            }

            public override bool ParseItem(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return default;
                }
                return bool.Parse(value);
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                var dic = Parse(value);
                binaryWriter.Write(dic.Count);
                foreach (var item in dic)
                {
                    binaryWriter.Write(item.Key);
                    binaryWriter.Write(item.Value);
                }
            }
        }
    }
}
